namespace Chaliy.Syndication.UnitTests
{
	using System;
	using System.Xml.XPath;
    using System.Runtime.Serialization;
    using System.IO;
		
	internal class ElementSerializer
	{
		#region Fields
		
		private readonly object element;			
		
		#endregion
		
		#region Constructors
		
		public ElementSerializer(object element)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			
			this.element = element;
		}
		
		#endregion
		
		#region Properties
		
		protected object Element 
		{
			get 
			{
				return this.element; 
			}
		}
		
		#endregion
		
		#region Methods
		
		public IXPathNavigable Serialize()
		{
            DataContractSerializer serializer 
                = new DataContractSerializer(this.Element.GetType());

            using (Stream memory = new MemoryStream())
            {
                serializer.WriteObject(memory, this.Element);

                memory.Position = 0;

                return new XPathDocument(memory);
            }			
		}
		
		#endregion
	}
}
